class_name ChatRoom extends Node2D

var prefab_label = preload("res://个人中心/聊天室/其他人聊天信息.tscn")
var prefab_me_label = preload("res://个人中心/聊天室/我的聊天信息.tscn")

func _init():
	ObjectManager.chatRoom = self

func _process(_delta):
	if(KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_ENTER)):
		_on_button_pressed()

func add_label(dic:Dictionary):
	var username = dic["data"]["username"]
	var is_me = username == ObjectManager.myUserInfo.username
	var message = dic["data"]["message"]
	var label:Label
	if is_me:
		label = prefab_me_label.instantiate()
	else:
		label = prefab_label.instantiate()
	label.text = username+": "+message
	$"ScrollContainer/VBoxContainer".add_child(label)
	await get_tree().create_timer(.1).timeout
	$"ScrollContainer".set_deferred("scroll_vertical", $"ScrollContainer/VBoxContainer".size.y)

# 发送聊天信息
func _on_button_pressed():
	var message = $"LineEdit".text
	if message == "":
		return
	var username = ObjectManager.myUserInfo.username
	WebSocketManager.sendObj(false,"chat",{"username":username,"message":message})
	$"LineEdit".text = ""
	
	
func update_online(number:int):
	$"Label".text = "当前在线人数："+String.num_int64(number)
